#! /bin/bash

if [ $# -eq 0 ]; then
    echo "Sintaxe: `basename $0` list-arqs"
    exit 10
fi

for full_file_name in "$@"; do
    echo -n "$full_file_name "

    if [ ! -f "$full_file_name" ]; then
        echo " nao existe!";
    else
        new_file_name=$PWD/M_`basename $full_file_name`
        cp -f $full_file_name $new_file_name 2> /dev/null

        # x and y resolution
        x_res=`identify -format "%[resolution.x]" $new_file_name`
        y_res=`identify -format "%[resolution.y]" $new_file_name`

        if [ "$x_res" != "$y_res" ]; then
            echo "Nao sei lidar com resolução X ($x_res) diferente de Y ($y_res)"
        else

            res=$x_res

            # width and height in pixels
            w_px=`identify -format "%w" $new_file_name`
            h_px=`identify -format "%h" $new_file_name`

            rotated=0
            # portrait?
            if [ $h_px -gt $w_px ]; then                
                rotated=1
                convert $new_file_name -rotate 90 $new_file_name
                tmp=$w_px
                w_px=$h_px
                h_px=$tmp    
            fi
            
            # width and height in inches
            w_in=$(( w_px / res ))
            h_in=$(( h_px / res ))

#            w_bk_in=$(( w_in + 2 ))
#            h_bk_in=$(( h_in + 2 ))

            # width and height of the background in inches
            w_bk_in=$(( w_in + (10 * w_in / 100) ))
            h_bk_in=$(( h_in + (10 * w_in / 100) ))

            if [ $w_bk_in -eq $w_in -o $h_bk_in -eq $h_in ]; then
                w_bk_in=$(( w_in + 2 ))
                h_bk_in=$(( h_in + 2 ))
            fi 
            
            # width and height of the background in pixels
            w_bk_px=$(( w_bk_in * res ))
            h_bk_px=$(( h_bk_in * res ))
#            w_bk_px=$(( w_px + 100))
#            h_bk_px=$(( h_px + 100 ))

#            echo "res = $res, w_px = $w_px, h_px = $h_px, w_in = $w_in, h_in = $h_in, w_bk_in = $w_bk_in, h_bk_in = $h_bk_in, w_bk_px = $w_bk_px, h_bk_px = $h_bk_px"

            # creating the background file
            convert $new_file_name -geometry "$w_bk_px"x"$h_bk_px" -quality 100 tmp1.jpg
            
            
#            convert -size "$w_bk_px"x"$h_bk_px" xc:white bkk.jpg
#            convert tmp1.jpg -quality 100 -alpha Opaque +level-colors white tmp2.jpg
            convert tmp1.jpg -alpha off -fill white -colorize 100%  -quality 100 tmp2.jpg
#            cmd="convert tmp.jpg -resize "$w_bk_px"x"$h_bk_px" bkk.jpg"
#            echo `$cmd`

            # inserting the image in the background file
            composite -quality 100 -gravity center $new_file_name tmp2.jpg tmp3.jpg

            # saving the final file
            mv tmp3.jpg $new_file_name 2> /dev/null

            # cleaning
            rm -f tmp*  2> /dev/null

#            echo "rotated = $rotated"
            # rotated?
            if [ "$rotated" -eq 1 ]; then
                convert $new_file_name -rotate -90 $new_file_name
            fi 
                
            echo " emoldurado"
            
        fi
    fi
done

#rm -f bkk.jpg 2> /dev/null



# arquivo imagem com nome correto?
#pattern=([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]\.jpg)
#if !([[ $img =~ $pattern ]]); then
#    echo "Arquivo $img nao tem o formato AAAA-MM-DD-NNNN.jpg"
#    exit 40
#fi








    

#convert "$img" -resize 1072x792  -resample 300x300 -quality 100 "$img"




